﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using Converter;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.Resources;
using System.Reflection;
using Accounting.Library;
using csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_KyGiaThanh;

namespace csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_CPTrucTiep
{
    public partial class FormThongTin : ChildWindow
    {
        GT_CPTrucTiep_ListViewModel ListViewModel;
        GT_CPTrucTiep_ChildWindow gt_CPTrucTiep_ChildWindow;
        public event EventHandler Saved;
        public FormThongTin(GT_CPTrucTiep_ListViewModel listViewModel)
        {        
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("ccsla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.GT_CPTrucTiep.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.ListViewModel = listViewModel;

            System.Windows.Data.CollectionViewSource gt_KyGiaThanh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KyGiaThanh_ListViewModelSource"];
            GT_KyGiaThanh_ListViewModel gt_KyGiaThanh_ListViewModel = new GT_KyGiaThanh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            gt_KyGiaThanh_CollectionViewSource.Source = new List<object> { gt_KyGiaThanh_ListViewModel };

           
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void TinhCPTrucTiepButton_Click(object sender, RoutedEventArgs e)
        {

            if (KyGiaThanh_ComboBox.SelectedItem == null)
            {
                MessageBoxResult notice = MessageBox.Show("Bạn chưa nhập liệu đầy đủ", "Chưa nhập liệu", MessageBoxButton.OKCancel);
            }
            else
            {

                IList<GT_CPTrucTiepInfo> list = ListViewModel.Model.getListOfInfo();
                GT_CPTrucTiepInfo CPTrucTiep = null;
                foreach (GT_CPTrucTiepInfo item in list)
                {
                    if (item.KyGiaThanhInfo == ((GT_KyGiaThanhInfo)this.KyGiaThanh_ComboBox.SelectedItem).Ma)
                    {
                        CPTrucTiep = item;
                    }
                }
                if (CPTrucTiep != null)
                {

                    MessageBoxResult result = MessageBox.Show("Chứng từ tập hợp chi phí trực tiếp này đã tồn tại, bạn có muốn xem hay không?", "Xem chứng từ tập hợp chi phí trực tiếp", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {

                        gt_CPTrucTiep_ChildWindow = new GT_CPTrucTiep_ChildWindow(CPTrucTiep.Khoa);
                        gt_CPTrucTiep_ChildWindow.Show();
                        gt_CPTrucTiep_ChildWindow.TinhCPTrucTiepCompleteted += new EventHandler(OnTinhCPTrucTiepCompleteted);
                        gt_CPTrucTiep_ChildWindow.OnClosedChildWindow += new EventHandler(OnClosedWindow);

                    }
                }
                else
                {

                    gt_CPTrucTiep_ChildWindow = new GT_CPTrucTiep_ChildWindow((GT_KyGiaThanhInfo)this.KyGiaThanh_ComboBox.SelectedItem, this.DienGiaiTextBox.Text);
                    gt_CPTrucTiep_ChildWindow.Show();
                    gt_CPTrucTiep_ChildWindow.Visibility = Visibility.Collapsed;

                    this.TinhCPTrucTiepItemButton.IsEnabled = false;
                    this.BoQuaItemButton.IsEnabled = false;
                    gt_CPTrucTiep_ChildWindow.TinhCPTrucTiepCompleteted += new EventHandler(OnTinhCPTrucTiepCompleteted);
                    gt_CPTrucTiep_ChildWindow.OnClosedChildWindow += new EventHandler(OnClosedWindow);
                }
            }
            
        }
        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            
            ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, null);
            
        }
        private void OnClosedWindow(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OnTinhCPTrucTiepCompleteted(object sender, EventArgs e)
        {
            if (gt_CPTrucTiep_ChildWindow.CountSave == 0)
            {

                MessageBoxResult result = MessageBox.Show("Chứng từ tập hợp chi phí trực tiếp đã xong, bạn có muốn xem chứng từ tập hợp chi phí trực tiếp không ?", "Xem chứng từ tập hợp chi phí trực tiếp", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {
                    gt_CPTrucTiep_ChildWindow.Visibility = Visibility.Visible;
                    gt_CPTrucTiep_ChildWindow.CountSave++;
                    if (Saved != null)
                    {
                        Saved(sender, e);
                    }

                }
                else
                {
                    gt_CPTrucTiep_ChildWindow.CountSave++;
                    gt_CPTrucTiep_ChildWindow.Close();
                    if (Saved != null)
                    {
                        Saved(sender, e);
                    }

                }
            }
            else
            {
                if (Saved != null)
                {
                    Saved(sender, e);
                }
            }
           
        }
        
    }
}

